﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Sellers;

namespace ZBJF.Warranty.H5.Controllers
{
    [LoginFilter(IsCheck = false)]
    public class RegisterController : Controller
    {
        // GET: Register
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public bool ValidatePhoneIsExist(string userPhone)
        {
            return ServiceFactory.SellerEmployeeServiceInstance.LoadByPhone(userPhone) != null;
        }
        [HttpPost]
        public bool IsSellerCodeExist(string sellerCode)
        {
            return ServiceFactory.SellersServiceInstance.LoadBySellerCode(sellerCode) != null;
        }
        [HttpPost]
        public ActionResult NextToComplete(SellerEmployeeModel model)
        {
            if (ValidatePhoneIsExist(model.UserPhone))
            {
                TempData["success"] = false;
                TempData["Msg"] = "您输入的手机号已被注册！";
                return RedirectToAction("Index");
            }
            var success = ServiceFactory.SendSmsVerificationCodeLogServiceInstance.ValidatePhoneCode(model.UserPhone,
               model.VerificationCode, 5);
            Session["model"] = model;
            if (success)
            {
                return RedirectToAction("CompleteRegister");
            }
            else
            {
                TempData["success"] = false;
                TempData["Msg"] = "您输入的验证码有误或已失效（请在收到验证码后5分钟内使用）！";
                return RedirectToAction("Index");
            }
        }

        public ActionResult CompleteRegister()
        {
            var model = Session["model"] as SellerEmployeeModel;
            return View(model);
        }
        [HttpPost]
        public ActionResult NextToConfirmRegistInfo(SellerEmployeeModel model)
        {
            var seller = ServiceFactory.SellersServiceInstance.LoadBySellerCode(model.SellerCode);
            if (seller == null)
            {
                TempData["success"] = false;
                TempData["Msg"] = "销售代码不正确，请重新输入";
                model.SellerCode = string.Empty;
                Session["model"] = model;
                return RedirectToAction("CompleteRegister");
            }
            model.SellerName = seller.SellersName;
            model.SellerId = seller.ID;
            model.SellerCode = seller.SellerCode;
            model.InsuranceCompanyBranch = seller.SellersOrganization.OrganizationName;//二级销售机构
            model.Password = CommonMethod.JiaMi(model.Password);
            var parentSellersOrganization =
                ServiceFactory.SellersOrganizationServiceInstance.LoadParentOrganization(seller.SellersOrganization.ParentOrganizationId);
            if (parentSellersOrganization != null)
            {
                model.InsuranceCompany = parentSellersOrganization.OrganizationName;//二级销售机构
            }
            Session["model"] = model;
            return RedirectToAction("ConfirmRegisterInfo");
        }

        public ActionResult ConfirmRegisterInfo()
        {
            var model = Session["model"] as SellerEmployeeModel;
            return View(model);
        }
        /// <summary>
        /// 保存用户注册信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveRegister(SellerEmployeeModel model)
        {
            var sellerEmployee = new SellerEmployee
            {
                SellerId = model.SellerId ?? 0,
                UserAccount = model.UserPhone,
                Password = model.Password,
                UserName = model.UserName,
                UserPhone = model.UserPhone,
                CertificateNO = model.CertificateNO,
                IsAvailable = true,
                CreateBy = model.UserPhone,
                CreateTime = DateTime.Now,
                LastModifyBy = model.UserPhone,
                LastModifyTime = DateTime.Now,
                InsuranceCompany = model.InsuranceCompany,
                InsuranceCompanyBranch = model.InsuranceCompanyBranch,
            };
            var success = ServiceFactory.SellerEmployeeServiceInstance.AddEntities(sellerEmployee).ID > 0;
            TempData["success"] = success;
            TempData["msg"] = success ? "注册成功！" : "注册失败！";

            return success ? RedirectToAction("Index", "Login") : RedirectToAction("Index");
        }
    }
}